package org.acme.spring.web.controller;

import org.acme.spring.web.common.R;
import org.acme.spring.web.domain.TUser;
import org.acme.spring.web.mapper.TUserMapper;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

@RestController
@RequestMapping("/user")
public class UserController {
	private TUserMapper userMapper;

	public UserController(TUserMapper userMapper) {
		this.userMapper = userMapper;
	}

	@GetMapping
	public R get() {
		return R.wrapResult("success", userMapper.selectList(null));
	}

	@PostMapping
	public R add(@RequestBody TUser user) {
		user.setId(UUID.randomUUID().toString().substring(0, 8));
		return R.wrapEffectResult(userMapper.insert(user));
	}

	@PutMapping
	public R update(@RequestBody TUser user) {
		return R.wrapEffectResult(userMapper.updateById(user));
	}

	@DeleteMapping("/{id}")
	public R remove(@PathVariable String id) {
		return R.wrapEffectResult(userMapper.deleteById(id));
	}
}
